﻿using Customer.API.Applcation.Command.FinanceCommand;
using Customer.Domain;
using Customer.Infrstructre.FinanceManager;
using MediatR;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Customer.API.Applcation.CommandHandlers.FinanceCommandHandler
{
    /// <summary>
    /// 新增财务类别中介者
    /// </summary>
    public class ADDFinanceCommandHandler:IRequestHandler<ADDFinanceCommand,int>
    {
        private readonly IFinanceTypeRepository _financeTypeRepository;

        public ADDFinanceCommandHandler(IFinanceTypeRepository financeTypeRepository)
        {
            _financeTypeRepository = financeTypeRepository;
        }

        public async Task<int> Handle(ADDFinanceCommand request, CancellationToken cancellationToken)
        {
            var res=await _financeTypeRepository.GetAsync(x=>x.Office_Id==request.Office_Id && x.User_Id==request.User_Id && x.FinanceTypeName ==request.FinanceTypeName);
            if (res.Count() > 0)
            {
                return -1;//财务类别名称已存在
            }
            var financeModel = new FinanceType() { FinanceTypeId=request.FinanceTypeId, FinanceTypeCode=request.FinanceTypeCode, FinanceTypeName=request.FinanceTypeName, FRremark=request.FRremark, Office_Id=request.Office_Id, User_Id=request.User_Id, AddName=request.AddName, AddTime=request.AddTime };
            return await _financeTypeRepository.AddAsync(financeModel);
        }
    }
}
